package com.kzc.controller.user;

import com.kzc.base.ResponseWrapper;
import com.kzc.bean.UserBean;
import com.kzc.mapper.user.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class RegisterController {

    @Autowired(required = false)
    UserMapper userMapper;

    @RequestMapping(value = "/register",method = RequestMethod.GET)
    public ResponseWrapper<UserBean> registerUser(HttpServletRequest request){

        // httpServletRequest.getParameter()
        ResponseWrapper<UserBean> responseWrapper = new ResponseWrapper<>();
        String phone = request.getParameter("phone");
        String pwd = request.getParameter("password");
        String nickName = request.getParameter("nickName");

        if(phone == null || phone.isEmpty()){
            responseWrapper.setStatus(-1);
            responseWrapper.setMessage("手机号为空!");
            return responseWrapper;
        }

        if(pwd == null || pwd.isEmpty()){
            responseWrapper.setStatus(-1);
            responseWrapper.setMessage("密码为空!");
            return responseWrapper;
        }

        if(nickName == null || nickName.isEmpty()){
            responseWrapper.setStatus(-1);
            responseWrapper.setMessage("昵称为空!");
            return responseWrapper;
        }


        UserBean userBean = userMapper.userLogin(phone);

        if(userBean != null){
            int code  = userMapper.registerUser(phone,pwd,nickName);
            responseWrapper.setStatus(code);
            responseWrapper.setMessage("成功");
        }else{
            responseWrapper.setStatus(-1);
            responseWrapper.setMessage("您已是快租车的会员，可直接登录");
        }
        return responseWrapper;

    }


}
